use crate::dao::user_dao::UserDao;
use crate::models::list_response::ListResponse;
use crate::models::response_object::ResponseObject;
use crate::models::user::User;

pub struct UserService {
    user_dao: UserDao,
}

impl UserService {
    pub fn new(user_dao: UserDao) -> Self {
        UserService { user_dao }
    }

    pub fn get_user(id: i32) -> ResponseObject<User> {
        // 模拟从数据库中获取用户
        let user = User::new(id, Some("John Doe"), Some("john.doe@example.com"));
        ResponseObject::success_with_data(user)
    }

    pub fn get_users() -> ListResponse<User> {
        // 模拟从数据库中获取用户列表
        let users = vec![
            User::new(1,  Some("John Doe"), Some("john.doe@example.com")),
            User::new(2, Some("Jane Smith"), Some("jane.smith@example.com")),
        ];
        ListResponse::with_list(users)
    }

    pub async fn get_all_users(&self) -> Result<ListResponse<User>, String> {
        match self.user_dao.get_all_users().await {
            Ok(users) => Ok(        ListResponse::with_list(users)),
            Err(err) => {
                println!("Query failed: {:?}", err); // 查询失败，打印错误信息
                Err("Failed to retrieve users".to_string())
            }
        }
    }

    // 其他业务逻辑方法
}
